/*
 * @Description:
 * @Version: 2.0
 * @Autor: wsh
 * @Date: 2023-06-20 10:14:50
 * @LastEditors: uphldjx 1373577124@qq.com
 * @LastEditTime: 2024-06-04 15:19:29
 */
import { getAreaObj, uploadType } from '@/utils/util'
import { getStore } from '@/utils/store'
import { cloneDeep } from 'lodash-es'
// 列表
let taskTypeList = []
let userInfo = getStore('userInfo')
if (userInfo.classifyId == 4 && userInfo.userType != 3) {
	if (userInfo.userType == 1) {
		// 机构
		taskTypeList = [
			{ label: '（机构）老人能力评估', value: '1', type: 'success' },
			{ label: '机构等级评定', value: '2', type: 'warning' },
		]
	} else if (userInfo.userType == 2) {
		// 运营单位
		taskTypeList = [
			{ label: '（居家）老人能力评估', value: '3', type: 'primary' },
			{ label: '机构等级评定', value: '2', type: 'warning' },
		]
	}
} else {
	// 其他
	taskTypeList = [
		{ label: '（居家）老人能力评估', value: '3', type: 'primary' },
		{ label: '（机构）老人能力评估', value: '1', type: 'success' },
		{ label: '机构等级评定', value: '2', type: 'warning' },
	]
}
const tableConfig = {
	stripe: true,
	border: true,
	maxHeight: '80vh',
	columns: [
		{
			label: '任务编号',
			prop: 'id',
		},
		{
			label: '任务名称',
			prop: 'taskName',
		},
		{
			label: '任务类型',
			prop: 'taskType',
			propName: 'taskTypeLabel',
			dicData: taskTypeList,
			type: 'select',
			custome: 'tag',
			// code:'OSF_TASK_TYPE',
			// api:'getDictItemList',
		},
		{
			label: '任务等级',
			prop: 'level',
			propName: 'levelLabel',
			code: 'OST_TASK_LEVEL',
			custome: 'tag',
			api: 'getDictItemList',
		},
		{
			label: '任务时间段',
			prop: 'taskTimeRange',
			type: 'daterange',
			formart: 'YYYY年MM月DD日',
			formartValue: 'YYYY-MM-DD',
		},
		{
			label: '下发对象',
			prop: 'evaluatorJson',
		},
		{
			label: '下发时间',
			prop: 'createTime',
		},
		{
			label: '操作',
			prop: 'menu',
			width: 300,
			fixed: 'right',
		},
	],
}

// 新增编辑
export const BaseFormConfig = {
	inline: false,
	showBtn: true,
	labelPosition: 'right',
	labelWidth: '120px',
	column: [
		{
			label: '任务名称',
			prop: 'taskName',
			span: 12,
			rules: [{ required: true, message: '请填写任务名称' }],
		},
		{
			label: '任务类型',
			prop: 'taskType',
			propName: 'taskTypeLabel',
			// code:'OSF_TASK_TYPE',
			// api:'getDictItemList',
			// type:'remoteSelect',
			type: 'select',
			dicData: taskTypeList,
			span: 12,
			control: (e) => {
				return {
					// 评估对象（机构）
					dpServiceOrgBaseList: {
						display: e == 2,
					},
					// 评估对象（机构老人）
					dpServiceObjectList: {
						display: e == 1,
					},
					// 评估对象（居家老人）
					serviceObjectHomeList: {
						display: e == 3,
					},
					// 评估人(机构)
					bscTaskEvaluatorInfoList: {
						display: (e == 1 || e == 2) && userInfo.classifyId != 5,
					},
					// 评估人（居家）
					homePractitionerList: {
						display: e == 3 || (userInfo.classifyId == 5 && e),
					},
				}
			},
			rules: [{ required: true, message: '请填写任务类型' }],
		},
		{
			label: '任务等级',
			prop: 'level',
			propName: 'levelLabel',
			code: 'OST_TASK_LEVEL',
			api: 'getDictItemList',
			type: 'remoteSelect',
			span: 12,
			rules: [{ required: true, message: '请填写任务等级' }],
		},
		{
			label: '任务时间段',
			prop: 'taskTimeRange',
			type: 'datetimerange',
			format: 'YYYY年MM月DD HH:mm:ss',
			valueFormat: 'YYYY-MM-DD HH:mm:ss',
			span: 12,
			rules: [{ required: true, message: '请填写任务时间段' }],
		},
		{
			label: '下发对象',
			prop: 'bscTaskEvaluatorInfoList',
			span: 24,
			rules: [{ required: true, message: '请选择下发对象' }],
		},
		// （居家）下发对象
		{
			label: '下发对象',
			prop: 'homePractitionerList',
			span: 24,
			rules: [{ required: true, message: '请选择下发对象' }],
		},
		// 类型为评估(居家)老人
		{
			label: '评估对象',
			prop: 'serviceObjectHomeList',
			span: 24,
			rules: [{ required: true, message: '请选择评估对象' }],
		},
		// 类型为评估(机构)老人
		{
			label: '评估对象',
			prop: 'dpServiceObjectList',
			span: 24,
			rules: [{ required: true, message: '请选择评估对象' }],
		},
		// 类型为评估机构
		{
			label: '评估对象',
			prop: 'dpServiceOrgBaseList',
			span: 8,
			rules: [{ required: true, message: '请选择评估对象' }],
		},
		{
			label: '任务附件',
			prop: 'taskAttachment',
			type: 'upload',
			fileType: uploadType.fileMetaType,
			labelPosition: 'left',
			showFileList: true,
			limit: 9,
			multiple: true,
			span: 24,
		},
	],
}
export const detailConfig = {
	inline: false,
	showBtn: false,
	labelPosition: 'right',
	labelWidth: '120px',
	column: [
		{
			label: '任务名称',
			prop: 'taskName',
			span: 8,
			rules: [{ required: true, message: '请填写任务名称' }],
		},
		{
			label: '任务类型',
			prop: 'taskType',
			propName: 'taskTypeLabel',
			// code:'OSF_TASK_TYPE',
			// api:'getDictItemList',
			// type:'remoteSelect',
			type: 'select',
			dicData: taskTypeList,
			span: 8,
			control: (e) => {
				return {
					// 评估对象（机构）
					dpServiceOrgBaseList: {
						display: e == 2,
					},
					// 评估对象（机构老人）
					dpServiceObjectList: {
						display: e == 1,
					},
					// 评估对象（居家老人）
					serviceObjectHomeList: {
						display: e == 3,
					},
					// 评估人(机构)
					bscTaskEvaluatorInfoList: {
						display: e == 1 || e == 2,
					},
					// 评估人（居家）
					homePractitionerList: {
						display: e == 3,
					},
				}
			},
			rules: [{ required: true, message: '请填写任务类型' }],
		},
		{
			label: '任务等级',
			prop: 'level',
			propName: 'levelLabel',
			code: 'OST_TASK_LEVEL',
			api: 'getDictItemList',
			type: 'remoteSelect',
			span: 8,
			rules: [{ required: true, message: '请填写任务等级' }],
		},
		{
			label: '任务时间段',
			prop: 'taskTimeRange',
			type: 'datetimerange',
			format: 'YYYY年MM月DD HH:mm:ss',
			valueFormat: 'YYYY-MM-DD HH:mm:ss',
			span: 12,
			rules: [{ required: true, message: '请填写任务时间段' }],
		},

		{
			label: '下发对象',
			prop: 'evaluatorJson',
			span: 8,
			rules: [{ required: true, message: '请选择下发对象' }],
		},
		// 类型为评估老人
		{
			label: '评估对象',
			prop: 'dpServiceObjectList',
			span: 8,
			rules: [{ required: true, message: '请选择评估对象' }],
		},
		// 类型为评估机构
		{
			label: '评估对象',
			prop: 'dpServiceOrgBaseList',
			span: 8,
			rules: [{ required: true, message: '请选择评估对象' }],
		},
		// （居家）下发对象
		// {
		// 	label: '下发对象',
		// 	prop: 'homePractitionerList',
		// 	span: 24,
		// 	rules: [{ required: true, message: '请选择下发对象' }],
		// },
		// 类型为评估(居家)老人
		{
			label: '评估对象',
			prop: 'serviceObjectHomeList',
			span: 24,
			rules: [{ required: true, message: '请选择评估对象' }],
		},
		{
			label: '任务附件',
			prop: 'taskAttachment',
			type: 'upload',
			fileType: uploadType.fileMetaType,
			labelPosition: 'left',
			showFileList: true,
			limit: 9,
			multiple: true,
			span: 24,
		},
	],
}

export const baseConfig = {
	pageName: '任务发布',
	// 数据字典
	dictcodes: ['OSF_TASK_TYPE', 'OST_TASK_LEVEL'],
	rowKey: 'id',
	addLabel: '新增',
	// 删除插入
	tipLabel: 'taskName',
	api: {
		index: 'view_bscTaskInfo_page',
		detail:'view_bscTaskInfo_view',
		delete: 'view_bscTaskInfo_del',
		add: 'view_bscTaskInfo_add',
		edit: 'view_bscTaskInfo_edit',
	},
	auth: {
		index: 'view_bscTaskInfo_page',
		detail:'view_bscTaskInfo_view',
		delete: 'view_bscTaskInfo_del',
		add: 'view_bscTaskInfo_add',
		edit: 'view_bscTaskInfo_edit',
		// 批量设置
		mulitSet: 'mulitSet',
	},
	// 当前行操作按钮
	menuBtn: [
		{
			icon: 'editor',
			label: '编辑',
			type: 'primary',
			operate: 'edit',
			auth: 'view_dpNewsInfo_edit',
			text: true,
			show: true,
		},
		{
			icon: 'detail',
			label: '详情',
			type: 'primary',
			operate: 'Detail',
			auth: '',
			text: true,
			show: true,
		},
		{
			icon: 'delete',
			label: '删除',
			type: 'danger',
			operate: 'delete',
			auth: 'view_bscTaskInfo_del',
			text: true,
			show: true,
		},
	],
	dialogConfig: {
		width: '85%',
	},
	// 搜索
	searchConfig: {
		inline: true,
		showBtn: true,
		labelPosition: 'left',
		column: [
			{
				label: '任务编号',
				prop: 'id',
				span: 8,
			},
			{
				label: '任务类型',
				prop: 'taskType',
				propName: 'taskTypeLabel',
				// code:'OSF_TASK_TYPE',
				// api:'getDictItemList',
				dicData: taskTypeList,
				type: 'select',
				span: 8,
			},
			{
				label: '添加时间',
				prop: 'createTimeBetween',
				type: 'datetimerange',
				format: 'YYYY年MM月DD HH:mm:ss',
				valueFormat: 'YYYY-MM-DD HH:mm:ss',
				span: 16,
			},
		],
	},
	// 新增
	formConfig: {
		...BaseFormConfig,
		showBtn: true,
	},
	// 列表
	tableConfig,
	// 详情
	detailConfig: BaseFormConfig,
	// 处理发布日期
	beforeOnLoadFormate: async (params) => {
		if (params.createTimeBetween) {
			params.createTimeBetween = params.createTimeBetween.join(',')
		}
		const areasInfo: any = getAreaObj(params.areas_name, '')
		return Promise.resolve({
			...params,
			...areasInfo,
		})
	},
	operateFormate:async (form,type) =>{
		let item = cloneDeep(form)
		let taskTimeRange = []
			if (item.startTime && item.endTime) {
				taskTimeRange = [item.startTime, item.endTime]
			}
			if (item.evaluatorJson) {
				let list = JSON.parse(item.evaluatorJson)

				let nameList = list.map((el) => {
					return el.name
				})
				item.evaluatorJson = nameList.join(',')
			}
			const areasInfo: any = getAreaObj(item, '')
			return Promise.resolve({
				...item,
				...areasInfo,
				taskTimeRange,
			})
	},
	onLoadFormate: async (arr) => {
		const tableData = arr.map((item) => {
			let taskTimeRange = []
			if (item.startTime && item.endTime) {
				taskTimeRange = [item.startTime, item.endTime]
			}
			if (item.evaluatorJson) {
				let list = JSON.parse(item.evaluatorJson)

				let nameList = list.map((el) => {
					return el.name
				})
				item.evaluatorJson = nameList.join(',')
			}
			const areasInfo: any = getAreaObj(item, '')
			return {
				...item,
				...areasInfo,
				taskTimeRange,
			}
		})
		return Promise.resolve(tableData)
	},
	// 新增处理
	saveFormate: async (form) => {
		return new Promise((resolve, reject) => {
			const areasObj: any = getAreaObj(form.areas, '')
			// 处理时间段
			if (form.taskTimeRange) {
				form.startTime = form.taskTimeRange[0]
				form.endTime = form.taskTimeRange[1]
			}
			const params = {
				...form,
				...areasObj,
				areas: undefined,
				taskTimeRange: undefined,
			}
			resolve(params)
		})
	},
}
